import { _decorator, Component, director, instantiate, Label, Node, ProgressBar } from 'cc';
import { TaskData } from '../../Init/Managers/TaskSDK/任务管理类/TaskData';
import { SDKTools } from '../../Init/Managers/TaskSDK/SDKTools';
import { WebDataProxy } from '../../Init/Managers/TaskSDK/数据代理类/WebDataProxy';
import { ADMgr } from '../../Init/Managers/TaskSDK/广告播放类/ADMgr';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { TipClosedMode } from '../TipUI/TipUI';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
const { ccclass, property } = _decorator;

@ccclass('TaskCell')
export class TaskCell extends Component {
    @property(Label)
    TaskName: Label = null;

    @property(Label)
    TaskBarText: Label = null;

    @property(ProgressBar)
    TaskBarView: ProgressBar = null;

    @property(Node)
    Yi_Wan_Cheng_Btn: Node = null;

    @property(Node)
    Wei_Wan_Cheng_Btn: Node = null;

    @property(Label)
    TaskOk: Label = null;

    @property(Node)
    AwardList: Node = null;

    public _index: number = -1;

    public _data: TaskData = null;

    public SetUI(index: number, dat: TaskData) {
        this.node.active = true;
        this._index = index;
        this._data = dat;
        this.TaskName.string = `任务${SDKTools.GetShuZi(index + 1)}`;
        this.UpdateUIState();
        // 更新奖励信息
        for (let i = 0; i < this.AwardList.children.length; i++) {
            this.AwardList.children[i].active = i < (index + 1) * 2;
        }
    }

    public UpdateUIState() {
        let dat = this._data;
        this.TaskBarText.string = `${dat.m_TaskFinishCount}/${dat.m_TaskCount}`;
        this.TaskBarView.progress = dat.m_TaskFinishCount / dat.m_TaskCount;
        this.Yi_Wan_Cheng_Btn.active = dat.m_CheckTaskState == "完成";
        this.Wei_Wan_Cheng_Btn.active = dat.m_CheckTaskState == "未完成";

        this.TaskOk.string = this._data.m_okTime;
        this.TaskOk.node.active = this._data.m_CheckTaskState == "完成";
        this.AwardList.active = dat.m_CheckTaskState == "未完成";
    }

    private ad_unit: string = "";

    public async Wei_Wan_Cheng_Btn_Click() {
        if (SDKTools.isdebug) {
            WebDataProxy.inst.GetTaskData(this._index).UpDataTaskCount();
            if (this._data.m_CheckTaskState == "完成") {
                // 发放奖励
                StorageSystem.setData((d) => {
                    d.userAssets.asset += Math.floor(50 * 2);
                }, true);
                StorageSystem.updateToAssets(true, false);
            }
            return;
        }
        let adDat = WebDataProxy.inst.AdWebData.GetPlayAd();
        this.ad_unit = adDat.ad_unit;
        ADMgr.clear();
        if (!adDat.force_click) {
            this.no_force_click();
            return;
        }
        this.yes_force_click();
    }

    private async no_force_fun(res: TipClosedMode) {
        if (res == 0) {
            return;
        }
        let dat = await ADMgr.PlayAd({
            adUnitId: this.ad_unit,
            showLoading: true,
            showError: true,
        })
        if (dat.res != 1) {
            UISystem.showUI(UIEnum.TipUI, {
                tip: "激励视频需要完整播放才能获得奖励",
                showMode: "close",
                closeCallback: () => {
                    director.emit(EventTypes.SDKEvents.ShowInsertAd)
                }
            });
            return;
        }
        console.log("签到成功");
        WebDataProxy.inst.AdWebData.UpdatePlayAdCount(this.ad_unit);
        WebDataProxy.inst.GetTaskData(this._index).UpDataTaskCount();
        if (this._data.m_CheckTaskState == "完成") {
            StorageSystem.setData((d) => {
                d.userAssets.asset += Math.floor(50 * 2);
            }, true);
            StorageSystem.updateToAssets(true, false);
            // 发放奖励
            // storage.Instance.setEnergy(storage.Instance.nowEnery + (this._index + 1) * 2)
            // if (this.topuisc) {
            //     this.topuisc.setTiLi()
            // }
        }
    }

    public async no_force_click() {
        // 不需要强制点击
        let msg = WebDataProxy.inst.AdWebData.m_AdWebData.normal_tips;
        UISystem.showUI(UIEnum.TipUI, {
            tip: msg,
            showMode: "ad",
            closeCallback: this.no_force_fun.bind(this)
        });

    }

    private async yes_force_fun(res: TipClosedMode) {
        if (res == TipClosedMode.Close) {
            return;
        }
        let cls = SDKTools.m_ADClass
        if (SDKTools.hmCheck) {
            // ios没有后台监听的api 自定义时间戳判断
            ADMgr.start_time = SDKTools.NowTime;
        } else {
            // 安卓有后台监听的api
            cls.onHide(this.onHide.bind(this))
            cls.onShow(this.onShow.bind(this))
        }
        let dat = await ADMgr.PlayAd({
            adUnitId: this.ad_unit,
            showLoading: true,
            showError: true,
        })
        cls.offHide(this.onHide.bind(this))
        cls.offShow(this.onShow.bind(this))
        if (dat.res != 1) {
            UISystem.showUI(UIEnum.TipUI, {
                tip: "激励视频需要完整播放才能获得奖励",
                showMode: "close",
                closeCallback: () => {
                    director.emit(EventTypes.SDKEvents.ShowInsertAd)
                }
            });
            return;
        }
        // 判断是否强点成功
        let isoOk = WebDataProxy.inst.AdWebData.m_IOS_Force_Click_Ok;
        let androidOk = WebDataProxy.inst.AdWebData.m_Android_Force_Click_Ok;
        if (SDKTools.hmCheck) {
            if (!isoOk) {
                UISystem.showUI(UIEnum.TipUI, {
                    tip: WebDataProxy.inst.AdWebData.m_AdWebData.not_clickad_tips,
                    showMode: "close",
                    closeCallback: () => {
                        director.emit(EventTypes.SDKEvents.ShowInsertAd)
                    }
                });
                return
            }
        } else {
            if (!androidOk) {
                UISystem.showUI(UIEnum.TipUI, {
                    tip: WebDataProxy.inst.AdWebData.m_AdWebData.not_clickad_tips,
                    showMode: "close",
                    closeCallback: () => {
                        director.emit(EventTypes.SDKEvents.ShowInsertAd)
                    }
                });
                return
            }
        }
        WebDataProxy.inst.AdWebData.UpdatePlayAdCount(this.ad_unit);
        WebDataProxy.inst.GetTaskData(this._index).UpDataTaskCount();
        if (this._data.m_CheckTaskState == "完成") {
            // 发放奖励
            StorageSystem.setData((d) => {
                d.userAssets.asset += Math.floor(50 * 2);
            }, true);
            StorageSystem.updateToAssets(true, false);
        }
    }

    public async yes_force_click() {
        // 判断是否点击了广告
        let msg = WebDataProxy.inst.AdWebData.m_AdWebData.force_click_tips;
        UISystem.showUI(UIEnum.TipUI, {
            tip: msg,
            showMode: "ad",
            closeCallback: this.yes_force_fun.bind(this)
        });
    }

    onHide() {
        ADMgr.start_time = SDKTools.NowTime;
    }

    onShow() {
        ADMgr.end_time = SDKTools.NowTime;
    }

}

